<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="edu.boun.cmpe.elele.cotrans.dbOperations.*" %>
<%@page import="java.util.ArrayList" %>
<%@page import="java.util.Iterator" %>

<%
    String transID= request.getParameter("transID");
    String userID= session.getAttribute("userid").toString();
   // String userID="2";
    String rate= request.getParameter("rate");

    DbOperations db= new DbOperations();
    //insert rating for the translation
    db.insertRating(transID, userID, rate);
    
    //check translation rate
    ArrayList<String> a=db.getTranslationRate(transID);
        if(a.size()!=0){//translation exists
            Integer peopleNo= Integer.parseInt(a.get(0));
            peopleNo++;
            Integer rateNo=Integer.parseInt(a.get(1));
            rateNo+=Integer.parseInt(rate);

            db.updateTranslationRate(transID, peopleNo.toString(), rateNo.toString());
        } else{//new translation
            db.insertTranslationRate(transID, "1", rate);
        }

    //propogate ratings to users
    //get users on same translation
    ArrayList<String> list= db.getUsersOnTrans(transID);
    Iterator<String> it= list.iterator();
    while(it.hasNext()){
        String id=it.next();
        //check user rate
        ArrayList<String> b=db.getUserRate(id);
        if(b.size()!=0){//translation exists
            Integer peopleNo= Integer.parseInt(b.get(0));
            peopleNo++;
            Integer rateNo=Integer.parseInt(b.get(1));
            rateNo+=Integer.parseInt(rate);

            db.updateUserRate(id, peopleNo.toString(), rateNo.toString());
        } else{//new translation
            db.insertUserRate(id, "1", rate);
        }
    }//while
%>